package com.tfswx.jcyw.sqlmodel.gic.operators;

import com.alibaba.fastjson.JSON;
import com.tfswx.jcyw.sqlmodel.api.Operator;
import com.tfswx.jcyw.sqlmodel.api.bean.Node;
import com.tfswx.jcyw.sqlmodel.api.bean.jdpz.OrderJdpz;
import com.tfswx.jcyw.sqlmodel.api.bean.table.OrderByElement;
import com.tfswx.jcyw.sqlmodel.api.bean.table.ParenthesedSelect;
import com.tfswx.jcyw.sqlmodel.api.bean.table.PlainSelect;
import com.tfswx.jcyw.sqlmodel.api.bean.table.Select;
import com.tfswx.jcyw.sqlmodel.api.bean.table.expression.Column;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 张述江
 * @date 2023/9/7
 */
public class DefaultOrderOperator implements Operator {
    @Override
    public String getName() {
        return "排序";
    }

    @Override
    public Select operate(Node node, List<Select> pResultList) {
        OrderJdpz orderJdpz = JSON.parseObject(node.getJdpz(), OrderJdpz.class);
        Select select = pResultList.get(0);

        PlainSelect select1 = new PlainSelect(new ParenthesedSelect(select));

        List<OrderByElement> orderByElements = new ArrayList<>();

        for (OrderJdpz.PxbdsItem pxbdsItem : orderJdpz.getPxbdsList()) {
            orderByElements.add(new OrderByElement(new Column(pxbdsItem.getBds().toString()), pxbdsItem.getSfsx()));
        }

        select1.setOrderByElements(orderByElements);

        System.out.println("执行了" + getName() + "操作");
        return select1;
    }
}
